package com.shycloud.mido.opter.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.opter.api.entity.StockInOrder;
import com.shycloud.mido.opter.api.vo.StockInOrderVO;
import com.shycloud.mido.opter.service.StockInOrderService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/order")
public class OrderController {

	/** 商品入库模块 service */
	private StockInOrderService inOrderService;

	/**
	 * 新建 入库订单
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/22 17:19
	 **/
	@PostMapping("saveInOrder")
	public R saveInOrder(@RequestBody StockInOrderVO stockInOrderVO) {
		//创建日期
		stockInOrderVO.setCreateTime(new Date());
		return new R(inOrderService.saveInOrder(stockInOrderVO));
	}

	/**
	 * 删除入库单
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/24 10:12
	 **/
	@GetMapping("delInOrder")
	public R delInOrder(Integer id) {
		return new R(inOrderService.removeById(id));
	}

	/**
	 * 查询入库单列表
	 *
	 * @param supplierName
	 * @param startDate
	 * @param endDate
	 * @param current
	 * @param size
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/23 9:35
	 **/
	@GetMapping("getInOrderList")
	public R getInOrderList(String supplierName, String startDate, String endDate, Integer current, Integer size, String isCommit) {

		//创建分页对象
		Page page = new Page(current == null ? 1 : current, size == null ? 10 : size);

		return new R(inOrderService.getInOrderList(supplierName, startDate, endDate, page, isCommit));

	}

	/**
	 * 提交入库订单
	 *
	 * @param id
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/25 19:25
	 **/
	@GetMapping("commitInOrder")
	public R commitInOrder(Integer id) {

		StockInOrder order = inOrderService.getById(id);

		order.setIsCommit("1");

		return new R(inOrderService.updateById(order));
	}

	/**
	 * 获取入库单数量
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/25 19:50
	 **/
	@GetMapping("getInorderCount")
	public R getInorderCount() {

		Map<String, Integer> resultMap = MapUtil.newHashMap();
		//合计入库单数量
		resultMap.put("allInOrderCount", inOrderService.count());
		//未提交入库单数量
		resultMap.put("notInOrderCount", inOrderService.count(new QueryWrapper<StockInOrder>().eq("is_commit", "0")));

		return new R(resultMap);
	}

}
